package com.ruoyi.web.controller.veterinary;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.veterinary.domain.VetOutbound;
import com.ruoyi.veterinary.service.IVetOutboundService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出货管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/veterinary/outbound")
public class VetOutboundController extends BaseController
{
    @Autowired
    private IVetOutboundService vetOutboundService;

    /**
     * 查询出货管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:list')")
    @GetMapping("/list")
    public TableDataInfo list(VetOutbound vetOutbound)
    {
        startPage();
        List<VetOutbound> list = vetOutboundService.selectVetOutboundList(vetOutbound);
        return getDataTable(list);
    }

    /**
     * 导出出货管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:export')")
    @Log(title = "出货管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VetOutbound vetOutbound)
    {
        List<VetOutbound> list = vetOutboundService.selectVetOutboundList(vetOutbound);
        ExcelUtil<VetOutbound> util = new ExcelUtil<VetOutbound>(VetOutbound.class);
        util.exportExcel(response, list, "出货管理数据");
    }

    /**
     * 获取出货管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:query')")
    @GetMapping(value = "/{outboundId}")
    public AjaxResult getInfo(@PathVariable("outboundId") Long outboundId)
    {
        return success(vetOutboundService.selectVetOutboundByOutboundId(outboundId));
    }

    /**
     * 新增出货管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:add')")
    @Log(title = "出货管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VetOutbound vetOutbound)
    {
        return toAjax(vetOutboundService.insertVetOutbound(vetOutbound));
    }

    /**
     * 出库操作（包含库存扣减）
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:add')")
    @Log(title = "出库操作", businessType = BusinessType.INSERT)
    @PostMapping("/process")
    public AjaxResult processOutbound(@RequestBody VetOutbound vetOutbound)
    {
        try {
            return toAjax(vetOutboundService.processOutbound(vetOutbound));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改出货管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:edit')")
    @Log(title = "出货管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VetOutbound vetOutbound)
    {
        return toAjax(vetOutboundService.updateVetOutbound(vetOutbound));
    }

    /**
     * 删除出货管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:outbound:remove')")
    @Log(title = "出货管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{outboundIds}")
    public AjaxResult remove(@PathVariable Long[] outboundIds)
    {
        return toAjax(vetOutboundService.deleteVetOutboundByOutboundIds(outboundIds));
    }

    /**
     * 生成出货单号
     */
    @GetMapping("/generateOrderNo")
    public AjaxResult generateOrderNo()
    {
        String orderNo = vetOutboundService.generateOutboundOrderNo();
        return success(orderNo);
    }
}